
package Email;

import java.security.Security;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class GMailManager extends Thread
{
    static String mailhost = "smtp.gmail.com";
    static String username = "usc.jug";
    static String password = "uscjavausergroup";

    static String subject = "" ; 
    static String body = "" ; 
    static String sender = username + "@gmail.com" ; 
    static String recipients = "" ;
    
    @Override
    public void run()
    {
        try
        {
            Security.addProvider( new com.sun.net.ssl.internal.ssl.Provider() );

            Properties props = new Properties();
            props.setProperty( "mail.transport.protocol" , "smtp" );
            props.setProperty( "mail.host" , mailhost );
            props.put( "mail.smtp.auth" , "true" );
            props.put( "mail.smtp.port" , "465" );
            props.put( "mail.smtp.socketFactory.port" , "465" );
            props.put( "mail.smtp.socketFactory.class" , "javax.net.ssl.SSLSocketFactory" );
            props.put( "mail.smtp.socketFactory.fallback" , "false" );
            props.setProperty( "mail.smtp.quitwait" , "false" );

            Session session = Session.getDefaultInstance( props ,
                    new javax.mail.Authenticator()
                    {
                        @Override
                        protected PasswordAuthentication getPasswordAuthentication()
                        {
                            return new PasswordAuthentication( sender , password );
                        }

                    } );

            MimeMessage message = new MimeMessage( session );
            message.setSender( new InternetAddress( sender ) );
            message.setSubject( subject );
            message.setContent( body , "text/plain" );

            if ( recipients.indexOf( ',' ) > 0 )
            {
                message.setRecipients( Message.RecipientType.TO , InternetAddress.parse( recipients ) );
            }
            else
            {
                message.setRecipient( Message.RecipientType.TO , new InternetAddress( recipients ) );
            }

            Transport.send( message );
        }
        catch ( Exception e )
        {
            e.printStackTrace();
        }
    }
    
    public static synchronized void sendMail( String subject , String body , String recipients )
    {
        GMailManager.subject = subject ;
        GMailManager.body = body ;
        GMailManager.recipients = recipients ;
        
        // Send Mail Through a Thread ...
        GMailManager thread = new GMailManager() ;
        thread.start() ;
    }
}
